import { build } from "esbuild";
import * as path from "path";

async function compilePreloads() {
    await build({
        // entryPoints: [
        //     "src/apps/Index/preload_index.ts",
        //     "src/apps/User/preload_user.ts",
        // ],
        // outdir: "dist_main/preloads",
        entryPoints:["src/apps/preload.ts",],
        outfile:"dist_main/preload.js",
        bundle: false,
        minify: false,
        platform: "node",
        target: "node12",
        format: "cjs",
        loader: { ".ts": "ts" }
    }).catch((error) => {
        console.error(error);
        process.exit(1);
    });
}

async function compileMain() {
    await build({
        entryPoints: ["src/main.ts"],
        outfile: "dist_main/main.js",
        bundle: true,
        minify:true,
        platform: "node",
        target: "node12",
        format: "cjs",
        loader: { ".ts": "ts" },
        external: ["electron", "path", "fs"], // bundle为 true 时，排除 electron 模块不进行打包
        // define: {
        //   'process.env.NODE_ENV': '"production"' // 可选：定义环境变量
        // },
        alias: {
          '@': path.resolve(__dirname, 'src'), // 添加目录别名
        }
    }).catch((error) => {
        console.error(error);
        process.exit(1);
    });
}

(async ()=>{
    await compilePreloads();
    await compileMain();
})();
